
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>php面向对象 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="11_php命名空间.html" />
    
    
    <link rel="prev" href="09_php函数.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01_php介绍.html">
            
                <a href="01_php介绍.html">
            
                    
                    php基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01_php介绍.html">
            
                <a href="01_php介绍.html">
            
                    
                    php介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02_PHP基础语法.html">
            
                <a href="02_PHP基础语法.html">
            
                    
                    php基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="03_php变量.html">
            
                <a href="03_php变量.html">
            
                    
                    php变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="04_php数据类型.html">
            
                <a href="04_php数据类型.html">
            
                    
                    php数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="05_php常量.html">
            
                <a href="05_php常量.html">
            
                    
                    php常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="06_php运算符.html">
            
                <a href="06_php运算符.html">
            
                    
                    php运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="07_php流程控制.html">
            
                <a href="07_php流程控制.html">
            
                    
                    php流程控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="08_php数组.html">
            
                <a href="08_php数组.html">
            
                    
                    php数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="09_php函数.html">
            
                <a href="09_php函数.html">
            
                    
                    php函数
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.10" data-path="10_php面向对象.html">
            
                <a href="10_php面向对象.html">
            
                    
                    php面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="11_php命名空间.html">
            
                <a href="11_php命名空间.html">
            
                    
                    php命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="12_php日期和时间.html">
            
                <a href="12_php日期和时间.html">
            
                    
                    php日期时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="13_php表单.html">
            
                <a href="13_php表单.html">
            
                    
                    php表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="14_php包含文件.html">
            
                <a href="14_php包含文件.html">
            
                    
                    php包含文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="15_php常用函数.html">
            
                <a href="15_php常用函数.html">
            
                    
                    php常用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../php高级/16_php常用变量函数.md">
            
                <span>
            
                    
                    php常用变量函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../php高级/01_php错误与异常.html">
            
                <a href="../php高级/01_php错误与异常.html">
            
                    
                    php错误与异常
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../php高级/02_PhpStorm本地断点调试.html">
            
                <a href="../php高级/02_PhpStorm本地断点调试.html">
            
                    
                    PhpStorm本地断点调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../php高级/03_php文件操作.html">
            
                <a href="../php高级/03_php文件操作.html">
            
                    
                    php文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../php高级/04_cookie机制.html">
            
                <a href="../php高级/04_cookie机制.html">
            
                    
                    cookie机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../php高级/05_session.html">
            
                <a href="../php高级/05_session.html">
            
                    
                    session
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../php高级/06_php画图.html">
            
                <a href="../php高级/06_php画图.html">
            
                    
                    php画图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../php高级/07_php加密机制.html">
            
                <a href="../php高级/07_php加密机制.html">
            
                    
                    php加密机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >php面向对象</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="php&#x9762;&#x5411;&#x5BF9;&#x8C61;">PHP&#x9762;&#x5411;&#x5BF9;&#x8C61;</h1>
<h2 id="php-&#x7C7B;&#x5B9A;&#x4E49;">PHP &#x7C7B;&#x5B9A;&#x4E49;</h2>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">phpClass</span> </span>{
    <span class="hljs-comment">//&#x7C7B;&#x7684;&#x53D8;&#x91CF;&#x4F7F;&#x7528; var &#x6765;&#x58F0;&#x660E;, &#x53D8;&#x91CF;&#x4E5F;&#x53EF;&#x4EE5;&#x521D;&#x59CB;&#x5316;&#x503C;&#x3002;</span>
    <span class="hljs-keyword">var</span> $var1;
    <span class="hljs-keyword">var</span> $var2 = <span class="hljs-string">&quot;constant string&quot;</span>;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myfunc</span> <span class="hljs-params">($arg1, $arg2)</span> </span>{
        <span class="hljs-comment">//&#x53D8;&#x91CF; $this &#x4EE3;&#x8868;&#x81EA;&#x8EAB;&#x7684;&#x5BF9;&#x8C61;&#x3002;</span>
        $this-&gt;name = <span class="hljs-string">&apos;lili&apos;</span>;
        [..]
    }
    [..]
}
</code></pre>
<h2 id="php-&#x4E2D;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;">PHP &#x4E2D;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;</h2>
<p>&#x7C7B;&#x521B;&#x5EFA;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; new &#x8FD0;&#x7B97;&#x7B26;&#x6765;&#x5B9E;&#x4F8B;&#x5316;&#x8BE5;&#x7C7B;&#x7684;&#x5BF9;&#x8C61;</p>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">person</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;&#x8DD1;&#x6B65;&quot;</span>;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">say</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;&#x8BF4;&#x8BDD;&quot;</span>;
    }
}

$zhangsan = <span class="hljs-keyword">new</span> person;
</code></pre>
<h2 id="&#x8C03;&#x7528;&#x6210;&#x5458;&#x65B9;&#x6CD5;">&#x8C03;&#x7528;&#x6210;&#x5458;&#x65B9;&#x6CD5;</h2>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">person</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;&#x8DD1;&#x6B65;&quot;</span>;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">say</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;&#x8BF4;&#x8BDD;&quot;</span>;
    }
}

$zhangsan = <span class="hljs-keyword">new</span> person;
$zhangsan-&gt;run();
$zhangsan-&gt;say()
</code></pre>
<h2 id="php-&#x6784;&#x9020;&#x51FD;&#x6570;">PHP &#x6784;&#x9020;&#x51FD;&#x6570;</h2>
<p>&#x6784;&#x9020;&#x51FD;&#x6570;&#x662F;&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4E3B;&#x8981;&#x7528;&#x6765;&#x5728;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x65F6;&#x521D;&#x59CB;&#x5316;&#x5BF9;&#x8C61;&#xFF0C; &#x5373;&#x4E3A;&#x5BF9;&#x8C61;&#x6210;&#x5458;&#x53D8;&#x91CF;&#x8D4B;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x5728;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x8BED;&#x53E5;&#x4E2D;&#x4E0E; new &#x8FD0;&#x7B97;&#x7B26;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<pre><code>function __construct( $par1, $par2 ) {
   $this-&gt;url = $par1;
   $this-&gt;title = $par2;
}
</code></pre><h3 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h3>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">person</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">($name,$age)</span></span>{
        $this-&gt;name = $name;
        $this-&gt;age = $age;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> $this-&gt;name.<span class="hljs-string">&quot;&#x559C;&#x6B22;&#x8DD1;&#x6B65;&quot;</span>;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">say</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> $this-&gt;name.<span class="hljs-string">&apos;&#x4ECA;&#x5E74;&apos;</span>.$this-&gt;age.<span class="hljs-string">&apos;&#x5C81;&apos;</span>;
    }
}

$zhangsan = <span class="hljs-keyword">new</span> person(<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>,<span class="hljs-number">18</span>);
$zhangsan-&gt;run();
$zhangsan-&gt;say()
</code></pre>
<h2 id="&#x6790;&#x6784;&#x51FD;&#x6570;">&#x6790;&#x6784;&#x51FD;&#x6570;</h2>
<p>&#x6790;&#x6784;&#x51FD;&#x6570;(destructor) &#x4E0E;&#x6784;&#x9020;&#x51FD;&#x6570;&#x76F8;&#x53CD;&#xFF0C;&#x5F53;&#x5BF9;&#x8C61;&#x7ED3;&#x675F;&#x5176;&#x751F;&#x547D;&#x5468;&#x671F;&#x65F6;&#xFF08;&#x4F8B;&#x5982;&#x5BF9;&#x8C61;&#x6240;&#x5728;&#x7684;&#x51FD;&#x6570;&#x5DF2;&#x8C03;&#x7528;&#x5B8C;&#x6BD5;&#xFF09;&#xFF0C;&#x7CFB;&#x7EDF;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x6790;&#x6784;&#x51FD;&#x6570;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__destruct</span><span class="hljs-params">()</span> </span>{

}
</code></pre>
<h2 id="&#x7EE7;&#x627F;">&#x7EE7;&#x627F;</h2>
<p>PHP &#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57; extends &#x6765;&#x7EE7;&#x627F;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;PHP &#x4E0D;&#x652F;&#x6301;&#x591A;&#x7EE7;&#x627F;&#xFF0C;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Parent</span> </span>{
   <span class="hljs-comment">// &#x4EE3;&#x7801;&#x90E8;&#x5206;</span>
}
</code></pre>
<h2 id="&#x8BBF;&#x95EE;&#x63A7;&#x5236;">&#x8BBF;&#x95EE;&#x63A7;&#x5236;</h2>
<p>PHP &#x5BF9;&#x5C5E;&#x6027;&#x6216;&#x65B9;&#x6CD5;&#x7684;&#x8BBF;&#x95EE;&#x63A7;&#x5236;&#xFF0C;&#x662F;&#x901A;&#x8FC7;&#x5728;&#x524D;&#x9762;&#x6DFB;&#x52A0;&#x5173;&#x952E;&#x5B57; public&#xFF0C;protected&#x6216; private&#x6765;&#x5B9E;&#x73B0;&#x7684;&#x3002;</p>
<p>public&#xFF08;&#x516C;&#x6709;&#xFF09;&#xFF1A;&#x516C;&#x6709;&#x7684;&#x7C7B;&#x6210;&#x5458;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x5730;&#x65B9;&#x88AB;&#x8BBF;&#x95EE;&#x3002; protected&#xFF08;&#x53D7;&#x4FDD;&#x62A4;&#xFF09;&#xFF1A;&#x53D7;&#x4FDD;&#x62A4;&#x7684;&#x7C7B;&#x6210;&#x5458;&#x5219;&#x53EF;&#x4EE5;&#x88AB;&#x5176;&#x81EA;&#x8EAB;&#x4EE5;&#x53CA;&#x5176;&#x5B50;&#x7C7B;&#x548C;&#x7236;&#x7C7B;&#x8BBF;&#x95EE;&#x3002; private&#xFF08;&#x79C1;&#x6709;&#xFF09;&#xFF1A;&#x79C1;&#x6709;&#x7684;&#x7C7B;&#x6210;&#x5458;&#x5219;&#x53EA;&#x80FD;&#x88AB;&#x5176;&#x5B9A;&#x4E49;&#x6240;&#x5728;&#x7684;&#x7C7B;&#x8BBF;&#x95EE;&#x3002;</p>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">person</span></span>{
    <span class="hljs-keyword">public</span> $role =<span class="hljs-string">&quot;&#x4EBA;&#x7C7B;&quot;</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">($name,$age)</span></span>{
        $this-&gt;name = $name;
        $this-&gt;age = $age;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> $this-&gt;name.<span class="hljs-string">&quot;&#x559C;&#x6B22;&#x8DD1;&#x6B65;&quot;</span>;
        <span class="hljs-keyword">echo</span> $this-&gt;role;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">say</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> $this-&gt;name.<span class="hljs-string">&apos;&#x4ECA;&#x5E74;&apos;</span>.$this-&gt;age.<span class="hljs-string">&apos;&#x5C81;&apos;</span>;
    }
    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">play</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> $this-&gt;name.<span class="hljs-string">&apos;&#x73A9;&#x6E38;&#x620F;&#x5F88;&#x597D;&apos;</span>;
    }
}

$zhangsan = <span class="hljs-keyword">new</span> person(<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>,<span class="hljs-number">18</span>);
var_dump($zhangsan);
$zhangsan-&gt;run();
$zhangsan-&gt;say();
<span class="hljs-comment">//$zhangsan-&gt;play();//&#x62A5;&#x9519; &#x65B9;&#x6CD5;&#x79C1;&#x6709;</span>
</code></pre>
<h2 id="&#x5E38;&#x91CF;">&#x5E38;&#x91CF;</h2>
<p>&#x53EF;&#x4EE5;&#x628A;&#x5728;&#x7C7B;&#x4E2D;&#x59CB;&#x7EC8;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;&#x7684;&#x503C;&#x5B9A;&#x4E49;&#x4E3A;&#x5E38;&#x91CF;&#x3002;&#x5728;&#x5B9A;&#x4E49;&#x548C;&#x4F7F;&#x7528;&#x5E38;&#x91CF;&#x7684;&#x65F6;&#x5019;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528; $ &#x7B26;&#x53F7;&#x3002;</p>
<p>&#x5E38;&#x91CF;&#x7684;&#x503C;&#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x5B9A;&#x503C;&#xFF0C;&#x4E0D;&#x80FD;&#x662F;&#x53D8;&#x91CF;&#xFF0C;&#x7C7B;&#x5C5E;&#x6027;&#xFF0C;&#x6570;&#x5B66;&#x8FD0;&#x7B97;&#x7684;&#x7ED3;&#x679C;&#x6216;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x3002;</p>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>
</span>{
    <span class="hljs-keyword">const</span> role = <span class="hljs-string">&apos;&#x5E38;&#x91CF;&#x503C;&apos;</span>;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">showRole</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">echo</span>  <span class="hljs-keyword">self</span>::role ;
    }
}

<span class="hljs-keyword">echo</span> Person::role;

$obj = <span class="hljs-keyword">new</span> Person();
$obj-&gt;showRole();

<span class="hljs-keyword">echo</span> $obj::role;
</code></pre>
<h2 id="final-&#x5173;&#x952E;&#x5B57;">Final &#x5173;&#x952E;&#x5B57;</h2>
<p>PHP 5 &#x65B0;&#x589E;&#x4E86;&#x4E00;&#x4E2A; final &#x5173;&#x952E;&#x5B57;&#x3002;&#x5982;&#x679C;&#x7236;&#x7C7B;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x88AB;&#x58F0;&#x660E;&#x4E3A; final&#xFF0C;&#x5219;&#x5B50;&#x7C7B;&#x65E0;&#x6CD5;&#x8986;&#x76D6;&#x8BE5;&#x65B9;&#x6CD5;&#x3002;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7C7B;&#x88AB;&#x58F0;&#x660E;&#x4E3A; final&#xFF0C;&#x5219;&#x4E0D;&#x80FD;&#x88AB;&#x7EE7;&#x627F;&#x3002;</p>
<pre><code class="lang-php">class Person(){
    final public function eat() {
       echo &quot;eat food!&quot;;
   }
}
</code></pre>
<h2 id="&#x8C03;&#x7528;&#x7236;&#x7C7B;&#x6784;&#x9020;&#x65B9;&#x6CD5;">&#x8C03;&#x7528;&#x7236;&#x7C7B;&#x6784;&#x9020;&#x65B9;&#x6CD5;</h2>
<p>PHP &#x4E0D;&#x4F1A;&#x5728;&#x5B50;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4E2D;&#x81EA;&#x52A8;&#x7684;&#x8C03;&#x7528;&#x7236;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x3002;&#x8981;&#x6267;&#x884C;&#x7236;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x5B50;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4E2D;&#x8C03;&#x7528; <code>parent::__construct()</code>&#x3002;</p>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parents</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">()</span>
    </span>{
        $this-&gt;name = <span class="hljs-string">&quot;zhangsan&quot;</span>;
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">child</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Parents</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-keyword">parent</span>::__construct();
        var_dump($this);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printName</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> $this-&gt;name;
    }
}

$c = <span class="hljs-keyword">new</span> child();
$c-&gt;printName();
</code></pre>
<blockquote>
<p>&#x5F53;&#x5B50;&#x7C7B;&#x8981;&#x4F7F;&#x7528; &#x6784;&#x9020;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x5728;&#x5B50;&#x7C7B;&#x8C03;&#x7528; <code>parent::__construct()</code>&#xFF0C;&#x5426;&#x5219;&#x7236;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x3002;</p>
</blockquote>
<h2 id="interface">interface</h2>
<blockquote>
<p>interface&#x662F;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D;&#x63A5;&#x53E3;&#x64CD;&#x4F5C;&#x7684;&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x529F;&#x80FD;&#x662F;&#x628A;&#x6240;&#x9700;&#x6210;&#x5458;&#x7EC4;&#x5408;&#x8D77;&#x6765;&#xFF0C;&#x7528;&#x6765;&#x88C5;&#x5C01;&#x4E00;&#x5B9A;&#x529F;&#x80FD;&#x7684;&#x96C6;&#x5408;&#x3002;&#x5B83;&#x597D;&#x6BD4;&#x4E00;&#x4E2A;&#x6A21;&#x677F;&#xFF0C;&#x5728;&#x5176;&#x4E2D;&#x5B9A;&#x4E49;&#x4E86;&#x5BF9;&#x8C61;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x7684;&#x6210;&#x5458;&#xFF0C;&#x901A;&#x8FC7;&#x7C7B;&#x6216;&#x7ED3;&#x6784;&#x6765;&#x5B9E;&#x73B0;&#x5B83;&#x3002;&#x63A5;&#x53E3;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x5B9E;&#x4F8B;&#x5316;&#x3002;&#x63A5;&#x53E3;&#x4E0D;&#x80FD;&#x5305;&#x542B;&#x6210;&#x5458;&#x7684;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#xFF0C;&#x53EA;&#x5B9A;&#x4E49;&#x6210;&#x5458;&#x672C;&#x8EAB;&#x3002;&#x63A5;&#x53E3;&#x6210;&#x5458;&#x7684;&#x5177;&#x4F53;&#x4EE3;&#x7801;&#x7531;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x7684;&#x7C7B;&#x63D0;&#x4F9B;&#x3002;&#x63A5;&#x53E3;&#x4F7F;&#x7528;interface&#x5173;&#x952E;&#x5B57;&#x8FDB;&#x884C;&#x58F0;&#x660E;&#x3002;</p>
</blockquote>
<p>&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#xFF08;interface&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x67D0;&#x4E2A;&#x7C7B;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x54EA;&#x4E9B;&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x4E0D;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x7684;&#x5177;&#x4F53;&#x5185;&#x5BB9;&#x3002;</p>
<p>&#x63A5;&#x53E3;&#x662F;&#x901A;&#x8FC7; interface &#x5173;&#x952E;&#x5B57;&#x6765;&#x5B9A;&#x4E49;&#x7684;&#xFF0C;&#x5C31;&#x50CF;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6807;&#x51C6;&#x7684;&#x7C7B;&#x4E00;&#x6837;&#xFF0C;&#x4F46;&#x5176;&#x4E2D;&#x5B9A;&#x4E49;&#x6240;&#x6709;&#x7684;&#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x7A7A;&#x7684;&#x3002;</p>
<p>&#x63A5;&#x53E3;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x6240;&#x6709;&#x65B9;&#x6CD5;&#x90FD;&#x5FC5;&#x987B;&#x662F;&#x516C;&#x6709;&#xFF0C;&#x8FD9;&#x662F;&#x63A5;&#x53E3;&#x7684;&#x7279;&#x6027;&#x3002;</p>
<h3 id="&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x7528;&#x63A5;&#x53E3;&#xFF1F;">&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x7528;&#x63A5;&#x53E3;&#xFF1F;</h3>
<ol>
<li>&#x5B9A;&#x89C4;&#x8303;&#xFF0C;&#x4FDD;&#x6301;&#x7EDF;&#x4E00;&#x6027;&#xFF1B;</li>
<li>&#x591A;&#x4E2A;&#x5E73;&#x7EA7;&#x7684;&#x7C7B;&#x9700;&#x8981;&#x53BB;&#x5B9E;&#x73B0;&#x540C;&#x6837;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x53EA;&#x662F;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x4E0D;&#x4E00;&#x6837;</li>
</ol>
<h3 id="&#x63A5;&#x53E3;&#x4F7F;&#x7528;&#x89C4;&#x8303;">&#x63A5;&#x53E3;&#x4F7F;&#x7528;&#x89C4;&#x8303;</h3>
<ol>
<li>&#x63A5;&#x53E3;&#x4E0D;&#x80FD;&#x5B9E;&#x4F8B;&#x5316;</li>
<li>&#x63A5;&#x53E3;&#x7684;&#x5C5E;&#x6027;&#x5FC5;&#x987B;&#x662F;&#x5E38;&#x91CF;</li>
<li>&#x63A5;&#x53E3;&#x7684;&#x65B9;&#x6CD5;&#x5FC5;&#x987B;&#x662F;public&#x3010;&#x9ED8;&#x8BA4;public&#x3011;&#xFF0C;&#x4E14;&#x4E0D;&#x80FD;&#x6709;&#x51FD;&#x6570;&#x4F53;</li>
<li>&#x7C7B;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x7684;&#x6240;&#x6709;&#x65B9;&#x6CD5;</li>
<li>&#x4E00;&#x4E2A;&#x7C7B;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x5B9E;&#x73B0;&#x591A;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x7528;&#x9017;&#x53F7;&#x9694;&#x5F00;</li>
<li>&#x63A5;&#x53E3;&#x53EF;&#x4EE5;&#x7EE7;&#x627F;&#x63A5;&#x53E3;&#x3010;&#x7528;&#x7684;&#x5C11;&#x3011;</li>
</ol>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">usb</span></span>{
    <span class="hljs-keyword">const</span> brand = <span class="hljs-string">&apos;siemens&apos;</span>;    <span class="hljs-comment">// &#x63A5;&#x53E3;&#x7684;&#x5C5E;&#x6027;&#x5FC5;&#x987B;&#x662F;&#x5E38;&#x91CF;</span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">connect</span><span class="hljs-params">()</span></span>;  <span class="hljs-comment">// &#x63A5;&#x53E3;&#x7684;&#x65B9;&#x6CD5;&#x5FC5;&#x987B;&#x662F;public&#x3010;&#x9ED8;&#x8BA4;public&#x3011;&#xFF0C;&#x4E14;&#x4E0D;&#x80FD;&#x6709;&#x51FD;&#x6570;&#x4F53;</span>
}
<span class="hljs-comment">// new usb();  // &#x63A5;&#x53E3;&#x4E0D;&#x80FD;&#x5B9E;&#x4F8B;&#x5316;</span>

<span class="hljs-comment">// &#x7C7B;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Android</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">usb</span></span>{
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">connect</span><span class="hljs-params">()</span></span>{  <span class="hljs-comment">// &#x7C7B;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x7684;&#x6240;&#x6709;&#x65B9;&#x6CD5;</span>
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&apos;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x7684;connect&#x65B9;&#x6CD5;&apos;</span>;
    }
}


<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">usbA</span></span>{
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">connect</span><span class="hljs-params">()</span></span>;
}

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">usbB</span></span>{
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">contact</span><span class="hljs-params">()</span></span>;
}

<span class="hljs-comment">// &#x7C7B;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x5B9E;&#x73B0;&#x591A;&#x4E2A;&#x63A5;&#x53E3;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">mi</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">usbA</span>,<span class="hljs-title">usbB</span></span>{
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">connect</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&apos;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x7684;connect&#x65B9;&#x6CD5;&apos;</span>;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">contact</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&apos;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x7684;contact&#x65B9;&#x6CD5;&apos;</span>;
    }
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="09_php函数.html" class="navigation navigation-prev " aria-label="Previous page: php函数">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="11_php命名空间.html" class="navigation navigation-next " aria-label="Next page: php命名空间">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"php面向对象","level":"1.2.10","depth":2,"next":{"title":"php命名空间","level":"1.2.11","depth":2,"path":"php/php基础/11_php命名空间.md","ref":"./php/php基础/11_php命名空间.md","articles":[]},"previous":{"title":"php函数","level":"1.2.9","depth":2,"path":"php/php基础/09_php函数.md","ref":"./php/php基础/09_php函数.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"php/php基础/10_php面向对象.md","mtime":"2020-04-23T09:59:52.390Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-23T11:06:38.883Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

